package org.fhm.zdte.common.service.log;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.LoggerContextListener;
import ch.qos.logback.core.spi.ContextAwareBase;
import ch.qos.logback.core.spi.LifeCycle;

import java.util.Objects;

/**
 * Customized log path.
 *
 * @author 谭波
 * @since 2024/3/27
 */
public class CustomLogPathContextListener extends ContextAwareBase implements LoggerContextListener, LifeCycle {

    /**
     * The key of LOG_PATH.
     */
    private static final String LOG_PATH_KEY = "LOG_PATH";

    /**
     * The key of VM-PARAMETER for a log path.
     */
    private static final String VM_LOG_KEY = "zdte.log.dir";

    @Override
    public boolean isResetResistant() {
        return false;
    }

    @Override
    public void onStart(LoggerContext loggerContext) {
    }

    @Override
    public void onReset(LoggerContext loggerContext) {
    }

    @Override
    public void onStop(LoggerContext loggerContext) {
    }

    @Override
    public void onLevelChange(Logger logger, Level level) {
    }

    @Override
    public void start() {
        String logPath = System.getProperty(VM_LOG_KEY);
        if (Objects.nonNull(logPath) && !logPath.isEmpty())
            getContext().putProperty(LOG_PATH_KEY, logPath);
    }

    @Override
    public void stop() {
    }

    @Override
    public boolean isStarted() {
        return false;
    }
}